<template>
  <div>
    <el-dialog :title="ruleForm.id == '' ? '新增账户' : '编辑账户'" :visible.sync="addEditIsDialog_c" @close="resetForm('formName')" width="800px">
      <el-form :model="ruleForm" :rules="rules" ref="formName" label-width="120px" class="demo-ruleForm" :class="{ 'form-rules-show': seeType }">
        <el-row>
          <!-- <el-col :span="12">
            <el-form-item label="所属院区：" prop="hcid">
              <el-select v-model="ruleForm.hcid" placeholder="请选择所属院区" clearable>
                <el-option v-for="item in regionArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="登录账户：" prop="username">
              <el-input v-model="ruleForm.username" placeholder="请输入登录账户" clearable maxlength="50" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="管理员权限：" prop="isadmin">
              <el-checkbox v-model="ruleForm.isadmin" :true-label="1" :false-label="0">是否为管理员</el-checkbox>
            </el-form-item>
          </el-col>
        </el-row>
        <div v-if="ruleForm.isadmin == 1">
          <el-row v-for="item in permissionsArr" :key="item.id">
            <el-col :span="24">
              <el-form-item :label="item.title + '：'">
                <el-checkbox-group v-model="ruleForm.group_id">
                  <el-checkbox v-for="itema in item.group" :label="itema.id" :key="itema.id">{{ itema.title }}</el-checkbox>
                </el-checkbox-group>
              </el-form-item>
            </el-col>
          </el-row>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')">关 闭</el-button>
        <el-button type="primary" @click="submitForm('formName')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import { getUserInfo, getAuth, userAdd, saveUser } from "@/api/system";

export default {
  props: {
    addEditId: {
      type: String,
      default: "",
    },
    addEditStatus: {
      type: String,
      default: "add",
    },
    addEditIsDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      ruleForm: {
        // 表单字段
        username: "",
        // hcid: "",
        isadmin: 0,
        group_id:[],
      },
      rules: {
        // 表单规则
        hcid: [
          { required: false, message: "请选择所属院区", trigger: "change" },
        ],
        username: [{ required: true, message: "请输入登录账户", trigger: "blur" }],
        isadmin: [{ required: true, message: "请选择是否为管理员", trigger: "change" }],
      },
      addEditIsDialog_c: false, // 弹窗是否显示
      seeType: false, // 是否禁用
      userType: "", // 用户类型（0超级管理员，1赛区管理员）
      regionArray: [], // 院区集合
      permissionsArr: [], // 权限集合
    };
  },
  watch: {
    addEditIsDialog(val) {
      this.addEditIsDialog_c = val;
      if (this.addEditIsDialog_c) {
        // this.userType = Number(window.localStorage.getItem("userType"));
        // this.matchAreaId = Number(window.localStorage.getItem("matchAreaId"));
        // if (this.userType == 1) {
        //   this.ruleForm.matchAreaId = this.matchAreaId;
        // }

        // 添加
        if (this.addEditStatus == "add") {
          this.ruleForm.id = "";
          this.seeType = false;
        }

        // 编辑
        if (this.addEditStatus == "edit") {
          this.ruleForm.id = this.addEditId;
          this.seeType = false;
          this.getDetail();
        }

        // 查看
        if (this.addEditStatus == "see") {
          this.ruleForm.id = this.addEditId;
          this.seeType = true;
          this.getDetail();
        }

        // 获取用户权限
        this.getAuth();
      }
    },
  },
  methods: {
    // 详情
    getDetail() {
      getUserInfo({ id: this.addEditId }).then((res) => {
        if (res.code == 200) {
          this.ruleForm = res.data;
        }
      });
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
         if (this.ruleForm.id == "") {
          userAdd(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });

              // 关闭弹窗
              this.$parent.closeAddEdit();
              // 刷新列表
              this.$parent.onSubmit();
            }
          });
         } else {
          saveUser(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });

              this.ruleForm.group_id = [];
              // 关闭弹窗
              this.$parent.closeAddEdit();
              // 刷新列表
              this.$parent.onSubmit();
            }
          });
         }
        }
      });
    },
    // 取消
    resetForm(formName) {
      this.ruleForm.group_id = [];
      this.$refs[formName].resetFields();
      this.$parent.closeAddEdit();
    },
    // 获取用户权限
    getAuth(){
      getAuth({}).then((res) => {
        if (res.code == 200) {
          this.permissionsArr = res.data;
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;
}
</style>
